/*******************************************************************************
*  Project      : Minigate Golfin                                 
*  Program ID   : home.js                                 
*  Description  : 클럽개별메인 
*
********************************************************************************
*  Program History                                                            
*  Date        Author    Description                                          
*  ----------  --------  -------------------------------------------------------- 
*  2012-06-20  이윤노    Created.  
*  2012-06-00  이윤노    Complete.  
*******************************************************************************/

jQuery(function($){

	// 동호회 정보
	function clsClubInfo()
	{
		this._HTMLDom = null;
		this.infoData = null;
		
		this._init();
	}
	
	clsClubInfo.prototype = {
		_init: function(){
			var $layer = $("div#container ul.self_c_name li:eq(0)");
			
			this._HTMLDom = {
				"$layer": $layer
				, "$banner": $("div#container div.self_club_visual")
				, "$view": $("div#container section#main_content div.golf_board div.club_creat table tbody")
			}
			
		}
		, setData: function(thisData){
			this.infoData = thisData;
			//this.display();
		}
		, display: function(){
			var $layer = this._HTMLDom["$layer"];
			var $banner = this._HTMLDom["$banner"];
			var thisClass = this;
			
			//this.isRole();
			
			$layer.contents().filter(function(){
				return (this.nodeType==3 && this.data.trim())
			}).after(document.objUtil.leftB(this.infoData["동호회명"], 50)).remove();
				
			$layer.find("span:eq(0)").html(getFormatDateYmd(this.infoData["개설일"],".") + "(" + this.infoData["개설요일"] + ")");
			$layer.find("span.personnel").html(getFormatNumber(this.infoData["가입자수"]));
			$layer.find("span.comment").html(getFormatNumber(this.infoData["글수"]));
			
			if(this.infoData["가입여부"]=="0"){
				$layer.find("span:eq(3) a img").attr("src","../images/btn/btn_out03.gif").click(function(event){
					event.preventDefault();
					$(thisClass).trigger("clubClose");
				});
			}else if(this.infoData["가입여부"]=="1"){
				$layer.find("span:eq(3) a img").attr("src","../images/btn/btn_join.gif").click(function(event){
					event.preventDefault();
					alert("승인대기중입니다.");
				});
			}else {
				$layer.find("span:eq(3) a img").attr("src","../images/btn/btn_join.gif").click(function(event){
					event.preventDefault();
					$(thisClass).trigger("clubJoin");
				});
			}
			
			$banner.find("img").attr("src",this.infoData["배너이미지"]).width("980").height("170");		
		}	
		// 가입하기 버튼
		, $getBtnJoin: function(){
			var $layer = this._HTMLDom["$layer"];			
			return $layer.find("span:eq(3) a");
		}
		, setClubInfoData: function(key, val){
			this.infoData[key] = val;	
		}
		, getClubInfoData: function(key){
			return this.infoData[key];	
		}
		, isRole: function(){
			if(this.infoData["가입여부"] == "0"){
				alert("동호회에 가입되지 않은 회원은 이용하실 수 없습니다.");
				document.location.href = "home.html?club_seq=" + this.infoData["동호회키"];
			}
		}
	}
	
	function clsClubTransUser(){
		this.listData = null;
		this._init();	
	}
	
	clsClubTransUser.prototype = {
		_init: function(){
			var $layer = $("div.pw_area02");			
			this._HTMLDom = {
				"$layer": $layer	
			}	
			$layer.hide();
		}
		, setData: function(thisData){
			this.listData = thisData;
			this.display();
		}
		, display: function(){
			this._HTMLDom["$layer"].find(" p:eq(1) strong.point_b").html(this.listData["닉네임"]);
			this.showTime(0);
		}
		, showTime: function(arg){
			this._HTMLDom["$layer"].hide().eq(arg).show();
		}
	}
	
	function clsComm()
	{
		this.clubInfo = null;
		this.clubInfoRequest = null;
		
		this.clubMyList = null;
		
		this._init();
	}
	
	clsComm.prototype = {
		_init: function(){
			this.clubInfoRequest = {
				"동호회키": null
				, "동호회명": null
			}
			
			this.clubData = {
				"동호회명": null
				, "가입자수": null
				, "라운딩키": null
				, "라운딩제목": null
				, "가입여부": null
				, "가입유형": null
			}
			
		}	
//------------------------ 동호회기본정보------------------------//		
		, getClubInfo: function(){
			var thisClass = this;
			var club_seq = this.clubInfoRequest["동호회키"];
			
			// ajax 시작			
			//parameter setting and run
			document.objGlobal.ajax({
				"sid":"CL001",
				"servicename":"getClubInfo",
			}, {
				  "clubSeq": club_seq
			}, function(ret){
				
				// callback and mapping
				if(ret["msp"]["contents"]["items"]){					
					var obj = ret["msp"]["contents"]["items"];
					
					thisClass.clubData = {
						"동호회명": obj["clubName"]
						, "동호회키": obj["clubSeq"]
						, "배너이미지": obj["bannerUrl"]
						, "가입자수": obj["memberCnt"]
						, "글수": obj["totalBoardCnt"]
						, "개설일": obj["regDate"]
						, "개설요일": obj["regDay"]
						, "가입여부": obj["clubMember"]["memberStatus"]
						, "가입질문": obj["acceptQuestion"]
						, "가입유형": obj["acceptYn"]
						, "내상태": obj["clubMember"]["adminFlag"]
					};				    
				}
				
				$(thisClass).trigger("evtFinishClubInfo");
			});			
			
		}
		, getClubInfoData: function(){
			return this.clubData;
		}
		, setClubInfoRequest: function(key,val){
			this.clubInfoRequest[key] = val;	
		}
		, getClubInfoRequest: function(key){
			return this.clubInfoRequest[key];	
		}
//------------------------ 동호회기본정보------------------------//		
//------------------------ 동호회양도------------------------//
		, UpdateClubTrans: function(arg){			
			var thisClass = this;
			var szClubSeq = arg["동호회키"];
			var szUserId = arg["아이디"];
		
			// ajax 시작			
			//parameter setting and run
			document.objGlobal.ajax({
				"sid":"CL001",
				"servicename": "requestClubTransfer"
			}, {
				"clubSeq": szClubSeq
				, "userId": szUserId
			}, function(ret){				
				
				// callback and mapping
				var resData = {
					"응답코드": (ret["msp"]["contents"]["serviceResult"] == "00000"?true:false)
					, "메세지": _gf.getResultMsg(ret)
				}				
				$(thisClass).trigger("evtFinishUpdateClubTrans", [resData]);
			},true);
		}
//------------------------ 동호회양도------------------------//
//------------------------ 양도쪽지발송------------------------//
		, UpdateClubTransSendmail: function(arg){			
			var thisClass = this;
			var szClubSeq = arg["동호회키"];
			var szUserId = arg["아이디"];
			/*		
			// ajax 시작			
			//parameter setting and run
			document.objGlobal.ajax({
				"sid":"CL001",
				"servicename": "requestClubTransfer"
			}, {
				"clubSeq": szClubSeq
				, "userId": szUserId
			}, function(ret){				
				
				// callback and mapping
				var resData = {
					"응답코드": (ret["msp"]["contents"]["serviceResult"] == "00000"?true:false)
					, "메세지": document.objGlobal.getResultMsg("requestClubTransfer", ret["msp"]["contents"]["serviceResult"])				
				}				
				$(thisClass).trigger("evtFinishUpdateClubTransSendmail", [resData]);
			});
			*/
			var resData = {
				"응답코드": true
				, "메세지": "쪽지발송완료"				
			}	
			$(thisClass).trigger("evtFinishUpdateClubTransSendmail", [resData]);
		}
//------------------------ 양도쪽지발송------------------------//
		 
	}
	
	function clsControl()
	{
		this.objComm = null;
		
		this.objClubInfo = null
		this.objClubTransUser=null;
		
		this.openerControl = null;
		
		this.club_seq = null;
		this.club_my = null;
		this.user_id = null;
		
		//document.objGlobal.setWindowSize();
	}
	
	clsControl.prototype = {
		_init: function(){
			this._load();
			
			var member_id = document.objUtil.getCookie("member_id");
			this.club_seq = document.objUtil.getParam("club_seq");
			this.club_my = (document.objUtil.getParam("club_my") == "Y") ? 1:0;
			this.user_id = document.objUtil.getParam("user_id");
			
			if(!this.club_seq) document.location.href = "../club/club.html";
			
			if(opener){
				if(opener.document.objControl){
					this.openerControl = opener.document.objControl;
				}
			}else {
				alert("비정상적인 접근입니다.");
				window.close();
				return;
			}
			
			if(!member_id) {
				if(confirm("로그인 후 이용하여 주세요.")){
					document.objGlobal.goLogin();						
				}else{
					document.location.href = "../club/club.html";
				}				
			}else if(!this.club_seq) {
				document.location.href = "../club/club.html";
			} 
			
			
			
			this.objComm = new clsComm();
			
			this.objClubInfo = new clsClubInfo();	
			
			this.objClubTransUser = new clsClubTransUser();
			this._bind();
			
			//동호회정보
			this.objComm.setClubInfoRequest("동호회키",this.club_seq); 
			this.objComm.getClubInfo();
			
			
		}
		, _load: function(){
			//양도완료
			$.ajax({
				url: "http://192.168.0.82/popup/pop_club_supply2.html"
				, type: "get"
				, dataType: "html"
				, async: false
				, success: function(ret, sta){
					var szInject = "";
					var sectionStartIndex = ret.indexOf('<div class="pw_area02');
					var sectionEndIndex = ret.indexOf("</div", sectionStartIndex);
					
					szInject = ret.substring(sectionStartIndex, sectionEndIndex) + "</div>";
					
					$("p.pop_close").before(szInject);
				}
				, error: function(xhr, status, errorThown){
					alert(errorThown);
				}
			});	
			
		}
		, _bind: function(){
			var thisClass = this;
						
			// 동호회정보 조회 완료이벤트
			$(this.objComm).bind("evtFinishClubInfo", function(){
				var thisData = thisClass.objComm.getClubInfoData();				
				thisClass.objClubInfo.setData(thisData);
				
				//양도자 사용자 정보조회
				document.objGlobal.getMemberInfo({
					"아이디": thisClass.user_id					
					, "콜백": function(res, info, arg){
						thisClass.objClubTransUser.setData(info);
					}
				});
			});
						
			//양도신청 완료이벤트
			$(this.objComm).bind("evtFinishUpdateClubTrans", function(event, resData){
				if(resData["응답코드"]){
					var submitData = {
						"동호회키": thisClass.club_seq
						, "아이디": thisClass.user_id
					}
					thisClass.objComm.UpdateClubTransSendmail(submitData);
				}else{
					alert("문제가 좀 있는거 같아요. 기다려줘요");	
				}
			});
			
			//양도쪽지 완료이벤트
			$(this.objComm).bind("evtFinishUpdateClubTransSendmail", function(event, resData){
				//alert(resData["메세지"]);				
				if(resData["응답코드"]){					
					thisClass.objClubTransUser.showTime(1);
				}else{
					alert("문제가 좀 있는거 같아요. 기다려줘요");	
				}
			});
			
			//양도신청-예 버튼 이벤트
			$("p.pop_bt:eq(0) img[alt=예]").click(function(event){
				if(!thisClass.openerControl) return;
				
				var submitData = {
					"동호회키": thisClass.club_seq
					, "아이디": thisClass.user_id
				}
				thisClass.objComm.UpdateClubTrans(submitData);
			});
			//양도신청-아니오 버튼 이벤트
			$("p.pop_bt:eq(0) img[alt=아니오]").click(function(event){
				if(!thisClass.openerControl) return;
				thisClass.openerControl.clubTransRet();
				self.close();
			});
			
			//양도완료-확인 버튼 이벤트
			$("p.pop_bt:eq(1) img").click(function(event){
				if(!thisClass.openerControl) return;
				thisClass.openerControl.clubTransRet();
				self.close();
			});
		}
	}
	
	document.objControl = new clsControl();
	document.objControl._init();	
});	